<?php

class Controller_Assembleia extends Controller_Layout {
	
	public function action_create()
	{
		if( $this->request->is_ajax() ){
			$condominio_id = arr::get ( $_GET, 'condominio' );
			$this->template->set("condominio_id", $condominio_id );
			$this->template->render('assembleias/create');
		}else{
			$this->template->title = __('Cadastro de assembleias');
	  		$this->template->content = View::factory('assembleias/create' );
			$this->template->content->condominios = ORM::factory('condominio')->find_all();
		}
	}
		
	public function action_edit()
	{
		$id = $this->request->param('id');
		
		if( $this->request->is_ajax() ){
			$condominio_id = arr::get ( $_GET, 'condominio' );
			$this->template->set("condominio_id", $condominio_id );
			$this->template->set("assembleia", ORM::factory('assembleia', $id) );
			$this->template->render('assembleias/edit');
		}else{
			$this->template->title = __('Cadastro de assembleias');
	  		$this->template->content = View::factory('assembleias/edit' );
			$this->template->content->assembleia = ORM::factory('assembleia', $id);
			$this->template->content->condominios = ORM::factory('condominio')->find_all();
		}
	}
	
	public function action_save()
	{
		$assembleia = ORM::factory( 'assembleia', arr::get ( $_POST, 'id' ) );
		parent::serializePost( $assembleia, $_POST );
		$assembleia->save();
		
		$info = ( arr::get ( $_POST, 'id' ) ) ? "a" : "s" ;
		$info .= $assembleia->id;
		
		if( $this->request->is_ajax() ){
			$_SESSION['condominio_id'] = $assembleia->condominio_id;
			$_SESSION['info'] = $info;
			$this->action_list();
		}else{
			$this->request->redirect('assembleia/list/?info='.$info);
		}
		
	}
	
	public function action_delete()
	{
		$assembleia = ORM::factory('assembleia', $this->request->param('id'));
		$condominio_id = $assembleia->condominio_id;
		$assembleia->delete();
	
		$info = "e".$this->request->param('id');
		
		if( $this->request->is_ajax() ){
			$_SESSION['condominio_id'] = $condominio_id;
			$_SESSION['info'] = $info;
			$this->action_list();
		}else{
			$this->request->redirect('assembleia/list/?info='.$info);
		}
	}

	public function action_list()
	{
		
		$list_assembleias = null;
		$condominio_id = arr::get ( $_GET, 'condominio_id' );
		
		if( $condominio_id == null && isset($_SESSION['condominio_id']) ){
			$condominio_id = $_SESSION['condominio_id'];
			unset($_SESSION['condominio_id']);
		}

		$assembleia = ORM::factory('assembleia'); // ORM object
		
		// Prepare pagination
		$page = max($this->request->param('id'), 1);
		$items = Controller_Layout::$per_list; // Per page
		$offset = ($items * ($page - 1));
		
		// Object Pagination
		$pagination = Pagination::factory(array(
			'current_page'   => array('source' => 'route', 'key' => 'id'),
			'uri_segment'    => 'list',
			'total_items'    => $assembleia->count_all(),
			'items_per_page' => $items,
		));
		
		// Data List - $list_assembleias = $assembleia->order_by( 'assembleia_codigo', 'ASC' )->limit( $items )->offset( $offset )->find_all();
		if( $condominio_id != null ){
			$list_assembleias = $assembleia->where("condominio_id", "=", $condominio_id )->order_by( 'data', 'DESC' )->find_all();
		}else{ 
			$list_assembleias = $assembleia->order_by( 'data', 'DESC' )->limit( $items )->offset( $offset )->find_all();
		}
		
		
		$msg = null;
		$msgClass = null;
		
		if( arr::get($_GET, "info") || isset($_SESSION['info']) ){
		
			$code = ( arr::get($_GET, "info") ) ? arr::get($_GET, "info") : $_SESSION['info'];
			
			if( isset($_SESSION['info']) ) unset($_SESSION['info']);
			
			$type = substr($code, 0, 1);
			$ormId = substr($code, 1);
			
			if( $type == "e" ){
				$msg = "assembleia #".$ormId." excluída";
			}else{
				$obj = ORM::factory( 'assembleia', $ormId );
				$msg = "assembleia #".$ormId." ";
				if( $type == "a" )
					$msg .= " alterada";
				else if( $type == "s" )
					$msg .= " cadastrada";
			}
			
			$msg .= " com sucesso!";
			$msgClass = Controller_Layout::$messagesClasses[$type];
		
		}
			
		if( $this->request->is_ajax() ){
			$this->template->set("condominio_id", $condominio_id);
			$this->template->set("assembleias", $list_assembleias);
			//$this->template->set("pagination", $pagination);
			if( $msg != null ){
				$this->template->set("msg", $msg);
				$this->template->set("msgClass", $msgClass);
				$this->template->set("isClosable", false);
			}
			$this->template->render('assembleias/list');
		}else{
			$this->template->content = View::factory('assembleias/list');		
			$this->template->content->assembleias = $list_assembleias;
			$this->template->content->pagination = $pagination;
			
			if( $msg != null ){
				$this->template->content->msg = $msg;
				$this->template->content->msgClass = $msgClass;
				$this->template->content->isClosable = true;
			}
		}
		
	}
	
	function action_add_topicos(){
		$id = $this->request->param('id');
		$obj = ORM::factory( 'assembleia', $id );
		$topicos = $obj->topicos->find_all();
		
		$this->template->set("assembleia_id", $id);
		$this->template->set("assembleia", $obj);
		$this->template->set("topicos", $topicos);
		$this->template->render('assembleias/add_topicos');
	}
	
}
